/********************************************************/
/*	Project:					*/
/*	Author:		gong_libin			*/
/*	Date:		2016_01_01			*/
/*	File:		GlbMongo.cpp			*/
/********************************************************/

#include "GlbMongo.h"

CGlbMongo::CGlbMongo()
{
	m_pstCur = NULL;
	m_pstCli = NULL;
	m_pstData = NULL;
	m_pstColl = NULL;
}

CGlbMongo::~CGlbMongo()
{
	m_pstCur = NULL;
	m_pstCli = NULL;
	m_pstData = NULL;
	m_pstColl = NULL;
}

void CGlbMongo::GlbMongoInit()
{
	mongoc_init();

	return;
}

void CGlbMongo::GlbMongoClean()
{
	mongoc_cleanup();

	return;
}

int CGlbMongo::GlbGetClient(char* pszUri)
{
	int iReturn = GLB__SUCCESS;

	if (NULL == (m_pstCli = mongoc_client_new(pszUri))) {
		GLB__ERROR("%s\n", strerror(errno));
		iReturn = GLB__FAILURE;
	}

	return iReturn;
}

int CGlbMongo::GlbGetDatabase(char* pszData)
{
	int iReturn = GLB__SUCCESS;

	if (NULL == (m_pstData = mongoc_client_get_database(m_pstCli, pszData))) {
		GLB__ERROR("%s\n", strerror(errno));
		iReturn = GLB__FAILURE;
	}

	return iReturn;
}

int CGlbMongo::GlbGetCollection(char* pszData, char* pszColl)
{
	int iReturn = GLB__SUCCESS;

	if (NULL == (m_pstColl = mongoc_client_get_collection(m_pstCli, pszData, pszColl))) {
		GLB__ERROR("%s\n", strerror(errno));
		iReturn = GLB__FAILURE;
	}

	return iReturn;
}

void CGlbMongo::GlbDestroyCursor()
{
	mongoc_cursor_destroy(m_pstCur);
	m_pstCur = NULL;

	return;
}

void CGlbMongo::GlbDestroyClient()
{
	mongoc_client_destroy(m_pstCli);
	m_pstCli = NULL;

	return;
}

void CGlbMongo::GlbDestroyDatabase()
{
	mongoc_database_destroy(m_pstData);
	m_pstData = NULL;

	return;
}

void CGlbMongo::GlbDropCollection()
{
	bson_error_t stErr;

	if (true != mongoc_collection_drop(m_pstColl, &stErr)) {
		GLB__ERROR("%s\n", stErr.message);
	}

	return;
}

void CGlbMongo::GlbDestroyCollection()
{
	mongoc_collection_destroy(m_pstColl);
	m_pstColl = NULL;

	return;
}

bool CGlbMongo::GlbCursorMore()
{
	return mongoc_cursor_more(m_pstCur);
}

char* CGlbMongo::GlbCursorError()
{
	bson_error_t stErr;
	char* pszErr = NULL;

	if (true == mongoc_cursor_error(m_pstCur, &stErr)) {
		pszErr = stErr.message;
	}

	return pszErr;
}

bson_t* CGlbMongo::GlbCursorNext()
{
	bson_t* pstBson = NULL;

	mongoc_cursor_next(m_pstCur, (const bson_t**)&pstBson);

	return pstBson;
}

void CGlbMongo::GlbCollClean()
{
	bson_t stQuery = BSON_INITIALIZER;

	GlbCollDelete(&stQuery);

	return;
}

void CGlbMongo::GlbCollTraverse()
{
	bson_t stQuery = BSON_INITIALIZER;

	m_pstCur = mongoc_collection_find(m_pstColl, MONGOC_QUERY_NONE, 0, 0, 0, &stQuery, NULL, NULL);

	return;
}

bool CGlbMongo::GlbCollInsert(bson_t* pstDoc)
{
	bson_error_t stErr;
	bool bReturn = true;

	if (true != (bReturn = mongoc_collection_insert(m_pstColl, MONGOC_INSERT_NONE, pstDoc, NULL, &stErr))) {
		GLB__ERROR("%s\n", stErr.message);
	}

	return bReturn;
}

bool CGlbMongo::GlbCollDelete(bson_t* pstDoc)
{
	bson_error_t stErr;
	bool bReturn = true;

	if (true != (bReturn = mongoc_collection_remove(m_pstColl, MONGOC_REMOVE_NONE, pstDoc, NULL, &stErr))) {
		GLB__ERROR("%s\n", stErr.message);
	}

	return bReturn;
}

bool CGlbMongo::GlbCollUpdate(bson_t* pstSel, bson_t* pstUpd)
{
	bson_error_t stErr;
	bool bReturn = true;

	if (true != (bReturn = mongoc_collection_update(m_pstColl, MONGOC_UPDATE_NONE, pstSel, pstUpd, NULL, &stErr))) {
		GLB__ERROR("%s\n", stErr.message);
	}

	return bReturn;
}

void CGlbMongo::GlbCollSelect(bson_t* pstQuery, bson_t* pstField)
{
	m_pstCur = mongoc_collection_find(m_pstColl, MONGOC_QUERY_NONE, 0, 0, 0, pstQuery, pstField, NULL);

	return;
}
